
from django.test import TestCase

# Create your tests here.



class  TestTStudent:
    # 让当前py文件连接到数据库
    @staticmethod
    def f_init():
        import os
        os.environ["DJANGO_SETTINGS_MODULE"] = "django08_01.settings"
        import django
        django.setup()
        pass

    @staticmethod
    def f_select01():
        from app02.models import TStudent
        from django.db import connection
        from django.db.models.query import QuerySet

        # 查找数据库表里所有的记录
        v_result01:QuerySet=TStudent.objects.all()
        print(v_result01)

        # 输出django所要执行的sql语句
        print(connection.queries)
        pass

    @staticmethod
    def f_select02():
        from app02.models import TStudent
        from django.db import connection
        from django.db.models.query import QuerySet

        # 查找数据库表里所有的记录
        v_result01:QuerySet=TStudent.objects.filter(student_id__gt=4)
        print(v_result01)

        # 输出django所要执行的sql语句
        print(connection.queries)
        pass


    @staticmethod
    def f_select03():
        from app02.models import TStudent
        from django.db import connection
        from django.db.models.query import QuerySet

        # 查找数据库表里所有的记录
        v_result01:QuerySet=TStudent.objects.all()
        v_result02=v_result01.count()
        print('查找出来的记录数为：',v_result02)
        # 输出django所要执行的sql语句
        print(connection.queries)
        pass

    pass

    @staticmethod
    def f_select07():
        from app02.models import TStudent
        from django.db import connection
        from django.db.models.query import QuerySet

        # 查找数据库表里所有的记录
        v_result01:QuerySet=TStudent.objects.all().order_by('student_id')
        v_result02=v_result01.first()
        v_result03=v_result01.last()
        v_result04=v_result01[3::2]

        print('第一条记录：',v_result02)
        print('最后一条记录：',v_result03)
        print('部分记录：',v_result04)
        # 输出django所要执行的sql语句
        print(connection.queries)
        pass


    @staticmethod
    def f_select08():
        from app02.models import TStudent
        from django.db import connection
        from django.db.models.query import QuerySet

        # 查找数据库表里所有的记录
        v_result01:QuerySet=TStudent.objects.all().order_by('student_id')

        v_result02:QuerySet=v_result01.values('student_name')


        print('查询结果为：',v_result02)
        # 输出django所要执行的sql语句
        print(connection.queries)
        pass

    pass

if __name__=='__main__':
    TestTStudent.f_init()
    # TestTStudent.f_save()
    TestTStudent.f_select08()

    pass